
export default class Tools{
    static initTools(){
        window.cesiumEventHandler = new Cesium.ScreenSpaceEventHandler(window.cesiumViewer.canvas);

        let getCartesian3FromPosition = function(pos,) {
            let ray = window.cesiumViewer.camera.getPickRay(pos);
            return window.cesiumViewer.scene.globe.pick(ray, window.cesiumViewer.scene);
        };


        window.cesiumEventHandler.setInputAction(evt => {
            let cartesian3 = getCartesian3FromPosition(evt.position);
            alert(cartesian3);
        }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
    }

    static convertXYZ2LLH(xyz){
        let sarr = xyz.split(",");
        if(sarr.length != 3) return;
        let  farr = [];
        sarr.forEach(function (s) {
            farr.push(parseFloat(s));
        });
        let cartesian = new Cesium.Cartesian3(farr[0],farr[1],farr[2]);
        const ellipsoid = window.cesiumViewer.scene.globe.ellipsoid || Cesium.Ellipsoid.WGS84;
        const cartographic = Cesium.Cartographic.fromCartesian(cartesian, ellipsoid);
        let lon = cartographic.longitude;
        let lat = cartographic.latitude;
        let height = cartographic.height;
        lon = Cesium.Math.toDegrees(lon);
        lat = Cesium.Math.toDegrees(lat);
        farr = [lon,lat,height];
        let res = farr.join(',');
        return res;
    }
    static convertLLH2XYZ(llh){
        let sarr = llh.split(",");
        if(sarr.length != 3) return;
        let  farr = [];
        sarr.forEach(function (s) {
            farr.push(parseFloat(s));
        });

        const car3 = Cesium.Cartesian3.fromDegrees(farr[0], farr[1],farr[2]);

        farr = [car3.x,car3.y,car3.z];
        let res = farr.join(',');
        return res;
    }
    static setFov(fov){
        fov = parseFloat(fov);
        window.cesiumViewer.scene.camera.frustum.fov =  Cesium.Math.PI * fov/180;
    }
    static setCameraPos(xyz){
        let sarr = xyz.split(",");
        if(sarr.length != 3) return;
        let  farr = [];
        sarr.forEach(function (s) {
            farr.push(parseFloat(s));
        });
        let c4 = new Cesium.Cartesian4(farr[0],farr[1],farr[2],1);
        let c = window.cesiumViewer.camera;
        Cesium.Matrix4.multiplyByVector(c.inverseViewMatrix,c4,c4);
        alert(c4);
    }
    static setW2C(xyz){
        let sarr = xyz.split(",");
        if(sarr.length != 3) return;
        let  farr = [];
        sarr.forEach(function (s) {
            farr.push(parseFloat(s));
        });
        let c4 = new Cesium.Cartesian4(farr[0],farr[1],farr[2],1);
        let c = window.cesiumViewer.camera;
        Cesium.Matrix4.multiplyByVector(c.viewMatrix,c4,c4);
        alert(c4);
    }
}